package com.simple.auth.service.impl;

import com.simple.auth.domain.dto.AuthParamsDto;
import com.simple.auth.domain.dto.RefreshTokenDto;
import com.simple.auth.domain.vo.AuthSuccessVO;
import com.simple.auth.enums.AuthType;
import com.simple.auth.service.AuthService;
import com.simple.auth.service.CaptchaService;
import com.simple.auth.service.RefreshTokenService;
import com.simple.common.constant.CommonConstants;
import com.simple.common.constant.SystemRedisConstans;
import com.simple.common.exception.ServiceException;
import com.simple.framework.core.redis.RedisService;
import com.simple.system.domain.vo.SysUserDetailsVo;
import com.simple.system.service.SysUserService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description 密码认证服务
 * @Author xiaoke
 * @Date 2024/8/10 18:00
 * @Version 1.0
 */
@Service("password_auth")
public class PasswordAuthServiceImpl implements AuthService, RefreshTokenService {

    private final SysUserService sysUserService;
    private final PasswordEncoder passwordEncoder;
    private final AuthServiceCommon authServiceCommon;
    private final RedisService redisService;
    private final CaptchaService captchaService;


    public PasswordAuthServiceImpl(SysUserService sysUserService, PasswordEncoder passwordEncoder, AuthServiceCommon authServiceCommon, RedisService redisService, CaptchaService captchaService) {
        this.sysUserService = sysUserService;
        this.passwordEncoder = passwordEncoder;
        this.authServiceCommon = authServiceCommon;
        this.redisService = redisService;
        this.captchaService = captchaService;
    }


    @Override
    public AuthSuccessVO login(AuthParamsDto dto) {
        dto.checkPassword();
        if (!captchaService.check(dto.getCodeKey(), dto.getCode())) {
            throw new ServiceException("验证码不正确");
        }
        SysUserDetailsVo userDetails = sysUserService.getUserNameDetails(dto.getUsername());
        if(userDetails.getPassword() == null) {
            throw new ServiceException("该用户没有设置密码");
        }
        if (!passwordEncoder.matches(dto.getPassword(), userDetails.getPassword())) {
            throw new ServiceException("用户或密码不正确");
        }
        redisService.deleteObject(SystemRedisConstans.SYS_MENU_ROUTER + userDetails.getId());

        List<String> userPerms = sysUserService.getUserPerms(userDetails.getId());
        return authServiceCommon.buildAuthSuccessVO(userDetails, userPerms, AuthType.SYSTEM);
    }

    @Override
    public AuthSuccessVO refresh(RefreshTokenDto dto) {
        Map<String, String> map = authServiceCommon.getRefreshMap(dto);
        if(map == null){
            return null;
        }
        String uuId = redisService.getCacheObject(CommonConstants.AUTH_REFRESH_TOKEN + map.get("username"));
        if(uuId == null || !uuId.equals(map.get("uuId"))) {
            return null;
        }
        SysUserDetailsVo userDetails = sysUserService.getUserNameDetails(map.get("username"));
        redisService.deleteObject(SystemRedisConstans.SYS_MENU_ROUTER + userDetails.getId());
        List<String> userPerms = sysUserService.getUserPerms(userDetails.getId());
        return authServiceCommon.buildAuthSuccessVO(userDetails, userPerms, AuthType.SYSTEM);
    }
}
